import 'package:flutter/material.dart';

class AnimatedSwitcherDemo extends StatefulWidget {
  @override
  _AnimatedSwitcherDemoState createState() => _AnimatedSwitcherDemoState();
}

class _AnimatedSwitcherDemoState extends State<AnimatedSwitcherDemo> {
  var _currentChild =
      Container(key: ValueKey('1'), height: 300, width: 300, color: Colors.red);
  @override
  Widget build(BuildContext context) {
    return Center(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        AnimatedSwitcher(
          duration: Duration(seconds: 2),
          switchInCurve: Curves.easeIn,
          child: _currentChild,
          transitionBuilder: (Widget child, Animation<double> value) {
            return ScaleTransition(
              child: child,
              scale: value,
            );
          },
          layoutBuilder: (Widget currentChild, List<Widget> previousChildren) {
            return Stack(
              children: <Widget>[...previousChildren, currentChild],
              alignment: Alignment.center,
            );
          },
        ),
        RaisedButton(
          onPressed: () {
            setState(() {
              _currentChild = Container(
                  key: ValueKey('2'),
                  height: 100,
                  width: 100,
                  color: Colors.blue);
            });
          },
        ),
      ],
    ));
  }
}
